package com.nsu.cl.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nsu.cl.pojo.Student;
import com.nsu.cl.service.StudentService;
import com.nsu.cl.util.MD5;
import com.nsu.cl.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "学生管理器")
@RestController
@RequestMapping("/studentController")
public class StudentController {
    
    @Autowired
    private StudentService studentService;
    
    //DELETE /sms/studentController/delStudentById

    @ApiOperation("编辑获取单个学生信息")
    @GetMapping("/getStudentById/{id}")
    public Result getStudentById(@ApiParam("学生ID") @PathVariable("id") Long id) {
        Student student = studentService.getStudentById(id);
        return Result.ok(student);
    }
    
    @ApiOperation("删除单个或多个学生信息")
    @DeleteMapping("/delStudentById")
    public Result delStudentById(@ApiParam("要删除的学生编号的JSON数组") @RequestBody List<Long> ids) {
        studentService.removeByIds(ids);
        return Result.ok();
    }
    
    // POST /sms/studentController/addOrUpdateStudent
    
    @ApiOperation("保存或者修改学生信息")
    @PostMapping("/addOrUpdateStudent")
    public Result addOrUpdateStudent(@ApiParam("要保存或修改的学生JSON") @RequestBody Student student) {
        Integer id = student.getId();
        if (null == id || 0 == id) {
            student.setPassword(MD5.encrypt(student.getPassword()));
        }
        studentService.saveOrUpdate(student);
        
        return Result.ok();
    }
    
    // sms/studentController/getStudentByOpr/1/3?name=&clazzName=
    
    @ApiOperation("分页带条件查询学生信息")
    @GetMapping("/getStudentByOpr/{pageNo}/{pageSize}")
    public Result getStudentByOpr(@ApiParam("页码数") @PathVariable("pageNo") Integer pageNo,
                                  @ApiParam("页码大小") @PathVariable("pageSize") Integer pageSize,
                                  @ApiParam("查询的条件") Student student) {
        // 分页信息封装Page对象
        Page<Student> pageParam = new Page<>(pageNo,pageSize);
        // 进心查询
        IPage<Student> studentIPage = studentService.getStudentByOpr(pageParam,student);
        // 分装Result返回
        return Result.ok(studentIPage);
    }

    @ApiOperation("通过条件查找学生")
    @GetMapping("/getStudentBySome")
    public Result getStudentBySome(@ApiParam("查询的条件") Student student) {
        System.out.println("student = " + student);
        return Result.ok(studentService.getStudentBySome(student));
    }
    
}
